#include "DebugRenderer.h"

DebugRenderer::DebugRenderer()
{
    this->physicsSDK = NULL;
}

DebugRenderer::~DebugRenderer()
{
}

void DebugRenderer::InitDebugRenderer(NxPhysicsSDK *physicsSDK)
{
    NxReal myScale = 1.0f;
    physicsSDK->setParameter(NX_VISUALIZATION_SCALE, myScale);
    this->physicsSDK = physicsSDK;
}

void DebugRenderer::ActivateDebugVisualization(NxParameter nxVisualizeParam)
{
    this->physicsSDK->setParameter(nxVisualizeParam, 1.0f);
}

void DebugRenderer::DeactivateDebugVisualization(NxParameter nxVisualizeParam)
{
    this->physicsSDK->setParameter(nxVisualizeParam, 0.0f);
}

void DebugRenderer::SetupColor(NxU32 color)
{   
    NxF32 Blue = NxF32((color)&0xff)/255.0f;
    NxF32 Green = NxF32((color>>8)&0xff)/255.0f;        
    NxF32 Red = NxF32((color>>16)&0xff)/255.0f;        
    glColor3f(Red, Green, Blue);    
}

void DebugRenderer::RenderDebugData(NxDebugRenderable* dbgRenderable)
{
    if(dbgRenderable == NULL) return;
    
    GLboolean hadLight = 0;
    glGetBooleanv(GL_LIGHTING, &hadLight);
    glDisable(GL_LIGHTING);
    glDisable(GL_TEXTURE_2D);

    glLineWidth(1.0f);

    // Render points       
    glPushMatrix();
    {            
        NxU32 nbPoints = dbgRenderable->getNbPoints();            
        const NxDebugPoint* points = dbgRenderable->getPoints();
        glBegin(GL_POINTS);
        while(nbPoints--)
        {                
            SetupColor(points->color);                
            glVertex3fv(&points->p.x);                
            points++;            
        }            
        glEnd();        
    }   
    glPopMatrix();

    // Render lines  
    glPushMatrix();
    {           
        NxU32 nbLines = dbgRenderable->getNbLines();            
        const NxDebugLine* lines = dbgRenderable->getLines();            
        glBegin(GL_LINES);            
        while(nbLines--)            
        {                
            SetupColor(lines->color);                
            glVertex3fv(&lines->p0.x);                
            glVertex3fv(&lines->p1.x);                
            lines++;            
        }            
        glEnd();        
    }    
    glPopMatrix();

    // Render triangles     
    glPushMatrix();
    {            
        NxU32 nbTris = dbgRenderable->getNbTriangles();            
        const NxDebugTriangle* triangles = dbgRenderable->getTriangles();            
        glBegin(GL_TRIANGLES);            
        while(nbTris--)            
        {                
            SetupColor(triangles->color);                
            glVertex3fv(&triangles->p0.x);                
            glVertex3fv(&triangles->p1.x);                
            glVertex3fv(&triangles->p2.x);                
            triangles++;            
        }            
        glEnd();        
    }    
    glPopMatrix();

    glColor3f(1.0f, 1.0f, 1.0f);   

    if(hadLight)
        glEnable(GL_LIGHTING);
}
